<?php
session_start();
extract($_POST);
extract($_REQUEST);
extract($_SESSION);

include("config.inc");
include("includes/database.php");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
global $table_name,$action;

?>
<form action="createform.php" method="post">
    Table Name : 
       <select name="table_name">
<?php
    $sql = "SHOW TABLES FROM $db_name";
$result = $db->query($sql);

if (!$result) {
    echo "DB Error, could not list tables\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}
$column_name="Table_in_".$db_name; 
while ($row = $db->fetch_array($result)) {
    extract($row);
    if (strcmp($row[$column_name],$table_name)==0){
      echo "<option value=\"$Tables_in_ustadqadb\" selected>$Tables_in_ustadqadb</option>\n";  
    } else {
    echo "<option value=\"$Tables_in_ustadqadb\">$Tables_in_ustadqadb</option>\n";
    }
}


    ?>
        </select>
    <input type="text" name="action" value="<?php echo $action;?>"/>
    <input type="submit" value="Kirim"/>
    
</form>
<?php
if (!empty($table_name)){
    global $db,$table_name;
    $sql="select * from $table_name";
    $result=$db->query($sql);
    $nf=mysql_num_fields($result);
    $html="
<form action=\\\"$action\\\" method=\\\"post\\\">    
    <input type=\\\"hidden\\\" name=\\\"action\\\" value=\\\"add_process\\\"/>
<table cellpadding=\\\"4\\\" cellspacing=\\\"0\\\">\r\n";
    $vars="";
    $insert="insert into $table_name values (null,";
    $edit="update $table_name set ";
    $table_header="<table width='100%' cellpadding='4' cellspacing='0'><tr>";
    $table_row="<tr>";
    for ($i=0;$i<$nf;$i++){
        $meta=mysql_fetch_field($result, $i);
        $html.= "<tr><td class=\\\"field_name\\\">".ucwords(str_replace("_"," ",$meta->name)).
                "</td><td class=\\\"field_object\\\"><input type=\\\"text\\\" name=\\\"$meta->name\\\" class=\\\"textbox\\\"/> </td></tr>\r\n";
        $vars.="$".$meta->name.",";
        $table_header.="<th>".ucwords(str_replace("_"," ",$meta->name))."</th>";
        $table_row.="<td>$".$meta->name."</td>";
        if ($i>0){
            $insert.="'$".$meta->name."',";
            $edit.="$meta->name='$".$meta->name."',";
        }
    }
    $insert.=")";
    $edit.=" where id=\$id";
    $html.="<tr><td></td><td><input class=\\\"button\\\" type=\\\"submit\\\" value=\\\"Kirim\\\"/>
        <input class=\\\"button\\\" type=\\\"button\\\" name=\\\"cancel\\\" 
        value=\\\"Cancel\\\" onclick=\\\"javascript:history.go(-1);\\\"/></td></tr></table></form>";
    $table_header.="<th>Action</th></tr></table>";
    $table_row.="<td><a href=\\\"?mod=\$mod&file=\$file&action=edit&id=\$id\\\">
        <img class=\\\"icon_small\\\" src='images/edit.png' title='Edit'/></a>
        <a href=\\\"?mod=\$mod&file=\$file&action=delete&id=\$id\\\"><img class=\\\"icon_small\\\" src='images/delete.png' title='Delete'/></a></td></tr>";
    echo "<textarea rows=20 cols=60>$html</textarea>";
echo "<textarea rows=5 cols=60>$vars</textarea>";
echo "<textarea rows=5 cols=60>$insert</textarea>";
echo "<textarea rows=5 cols=60>$edit</textarea>";
echo "<textarea rows=5 cols=60>$table_header</textarea>";
echo "<textarea rows=5 cols=60>$table_row</textarea>";
}

?>